package com.lhy.controller;


import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.lhy.domain.NewMessage;
import com.lhy.domain.Page;
import com.lhy.domain.Result;
import com.lhy.domain.Student;
import com.lhy.service.IUserService;



@Controller
@RequestMapping("/newmessagelist")
public class NewMessageListController {
	@Autowired
	private IUserService userService;


	@RequestMapping("")
	public ModelAndView list(HttpSession s,String pageNow){
		Integer pagenow = 1;
		if(!StringUtils.isEmpty(pageNow)){
			pagenow = Integer.valueOf(pageNow);
		}
		ModelAndView mv = new ModelAndView();
		try {
			Integer count = userService.queryAllNewMessageCount();
			Page page = new Page(count, pagenow);
			Integer startIndex = pagenow*page.getPageSize()-7;
			Integer endIndex = pagenow*page.getPageSize();
			List<NewMessage> list = userService.queryAllNewMessageByPage(startIndex,endIndex);
			
			
			mv.addObject("sequence",Integer.valueOf(s.getAttribute("usersequence").toString()));
			mv.addObject("list",list);
			mv.addObject("page",page);
			mv.setViewName("newmessagelist");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mv;
	}

	@RequestMapping("/do")
	public @ResponseBody
	Map<String, Object> add()  {
		Map<String, Object> returnMap = new HashMap<>();
		Result result=new Result();
		result.setCode("1");
		result.setDes("成功");     
		try{

			List<NewMessage>newMessageList = userService.queryNewMessageList();
			returnMap.put("newMessageList", newMessageList);
		}catch(Exception e){
			e.printStackTrace();
			result.setCode("0");
			result.setDes(e.getMessage());
		}
		
		returnMap.put("result", result);
		return returnMap;
	}
	@RequestMapping("/query")
	public ModelAndView query(HttpSession s,String author, String title, String startTime, String endTime){
		ModelAndView mv =new ModelAndView();
		mv.setViewName("newmessagelist");
		try{
			if(StringUtils.isEmpty(author)){
				author = null;
			}
			if(StringUtils.isEmpty(title)){
				title = null;
			}
			if(StringUtils.isEmpty(startTime)){
				startTime = null;
			}
			if(StringUtils.isEmpty(endTime)){
				endTime = null;
			}
			mv.addObject("sequence",Integer.valueOf(s.getAttribute("usersequence").toString()));
			List<NewMessage>newMessageList = userService.queryNewMessageListByPd(author,title,startTime,endTime);
			if(!StringUtils.isEmpty(author)){
				mv.addObject("checkAuthor",author);
			}
			if(!StringUtils.isEmpty(title)){
				mv.addObject("checkTitle",title);
			}
			if(!StringUtils.isEmpty(startTime)){
				mv.addObject("checkStartTime",startTime);
			}
			if(!StringUtils.isEmpty(endTime)){
				mv.addObject("checkEndTime",endTime);
			}
			
			mv.addObject("list",newMessageList);
		}catch(Exception e){
			e.printStackTrace();
			
		}
		return mv;
	}

	@RequestMapping("/delete")
	public @ResponseBody
	Map<String, Object> delete(String newMessage_id)  {
		Map<String, Object> returnMap = new HashMap<>();
		Result result=new Result();
		result.setCode("1");
		result.setDes("成功");     
		try{
		   
			userService.deleteNewMessageByPd(Integer.valueOf(newMessage_id));
			
		}catch(Exception e){
			e.printStackTrace();
			result.setCode("0");
			result.setDes(e.getMessage());
		}
		
		returnMap.put("result", result);
		return returnMap;
	}
	
}
